<?php
require_once 'requires.php';

session_start();

function replaceWords($line) {
    $wordsDic = array(
        '=====' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
        'reshuffle' => 'reshuffles',
        'clean' => 'cleans',
        'draw' => 'draws',
        'reveal' => 'reveals',
        'play' => 'plays',
        'start' => 'starts',
        'have' => 'has',
        'leave' => 'leaves',
        'gain' => 'gains',
        'buy' => 'buys',
        'get' => 'gets',
        'discard' => 'discards',
        'activate' => 'activates',
        'look' => 'looks',
        'return' => 'returns',
        'pay' => 'pays',
        'trash' => 'trashes',
        'take'=> 'takes',
        'name' => 'names',
        'put' => 'puts'
        
    );

    $wordsDicFixes = array(
        'revealsed' => 'revealed',
        'playsed' => 'played',
        'gainsed' => 'gained',
        'discardsed' => 'discarded',
        'activatesd' => 'activated',
    );

    foreach ($wordsDic as $key => $val) {
        $line = str_replace($key, $val, $line);
    }

    foreach ($wordsDicFixes as $key => $val) {
        $line = str_replace($key, $val, $line);
    }
    return $line;
}

function logPrint($line) {

    $lineParts = explode('|', $line);
    $linePartsIndex = 0;
    $output = "";
    
    while (count($lineParts) > 0) {
        $sentence = array_shift($lineParts);
        $sentenceUnits = explode("[!]", $sentence);
        
        $temp = $linePartsIndex;
        for ($index = 0; $index < count($sentenceUnits) - 1; $index++) {
            $replacementData = explode('~', array_shift($lineParts));
            $linePartsIndex++;
            //$replacementData[0] holds the key and $replacementData[1] holds the value
            $sentenceUnits[$index] .= $replacementData[1];
            
        }
        $output .= implode("", $sentenceUnits);

    }
    $output = replaceWords($output);
    echo $output;
}

if (!isset($_REQUEST['roomid'])) {
    $db = DBUtils::Create();
    $ids = $db->getAllLogsIDS();
    $curRoomid = 0;
    $participants = "";
    foreach ($ids as $logid_arr) {

        $roomid = $logid_arr['roomid'];
        $name = $logid_arr['name'];
        if ($name == "")
            continue;
        if ($curRoomid == 0)
            $curRoomid = $roomid;

        if ($curRoomid != $roomid) {
            $participants = substr($participants, 0, strlen($participants) - 2);
            $date = $db->getRoomDate($curRoomid);
            echo "<a href=\"log.php?roomid=$curRoomid\">$date - $participants</a>";
            echo "<br>";

            $curRoomid = $roomid;

            $participants = $name . ", ";
        } else {
            $participants .= $name . ", ";
        }
    }

    if ($curRoomid != 0) {
        $date = $db->getRoomDate($roomid);
        $participants = substr($participants, 0, strlen($participants) - 2);
        echo "<a href=\"log.php?roomid=$curRoomid\">$date - $participants</a>";
        echo "<br>";
    }
    die;
}

$roomID = $_REQUEST['roomid'];

if ($roomID <= 0) {
    echo "invalid room id";
    die;
}

$db = DBUtils::Create();
$isGameInSession = $db->isGameInSession($roomID);

if ($isGameInSession) {
    echo "Game in session";
    echo "<br>";
    echo "Don't try to cheat";
    die;
}

$logLines = $db->getLogMessages($roomID);
if (count($logLines) == 0) {
    echo "no game data available";
    die;
}
?>

<html>
    <head>
        <title>Game Log</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">       
        <style type="text/css">

            .center {
                margin:auto;
                width:70%;
                background-color:#b0e0e6;
                font-family: Arial, Helvetica, sans-serif;
                text-align:center;
            }

            table {
                text-align: left;
                font-size: 12px;
                font-family: verdana;
                background: #c0c0c0;
                text-align: left;
                margin-left:auto;
                margin-right:auto;
            }

            body {
                font-family: Arial, Helvetica, sans-serif;
                text-align:center;
            }

            table thead  {
                cursor: pointer;
            }

            table thead tr,
            table tfoot tr {
                background: #c0c0c0;
            }

            table tbody tr {
                background: #f0f0f0;
            }

            td {
                border: 1px solid white;
            }

        </style>
    </head>

    <body class="center">

        <h1>Log</h1>

        <table>
            <tbody>
                <?php
                foreach ($logLines as $logLine) {
                    $line = $logLine['line'];
                    if (substr_count($line, "NEW_LINE") == 0) {
                        echo '<tr><td>';
                        echo logPrint($line);
                        echo '</td></tr>';
                    } else {
                        echo '<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>';
                    }
                }
                ?>
            </tbody>
        </table>
        <div><p id="sec_debug" /></div>
    </body>
</html>